⚡️ Speed up function deserialize_primitive by 43%
#19
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 43% (0.43x) speedup for
deserialize_primitiveinsrc/datadog_api_client/model_utils.py⏱️ Runtime :
140 milliseconds→97.9 milliseconds(best of5runs)📝 Explanation and details
The optimization achieves a 43% speedup through two key changes:
1. Fast ISO date parsing for
datetype: The biggest performance gain comes from adding a fast path for ISO date strings (YYYY-MM-DD). Instead of always calling the expensiveparse(data)function, the code first checks if the string matches the common ISO format (length 10, hyphens at positions 4 and 7), then directly constructs the date usingdate(year, month, day). This optimization is dramatically effective for date parsing - test results show 1500-2300% speedups for date operations, reducing parse time from ~70-90μs to ~4-5μs per call.2. Identity checks instead of set membership: Replaced
klass in {datetime, date}withklass is datetime or klass is date(and similar changes for UUID/float). This eliminates the overhead of creating sets and computing hashes for type checking. While the per-call savings are smaller (~1-2μs), this adds up significantly across thousands of calls, showing 15-35% improvements in non-date operations.Why these optimizations work:
parse()function fromdateutilis designed for flexible date parsing but carries significant overhead for simple ISO datesis) is a simple pointer checkThe optimizations maintain identical behavior and error handling - the fast ISO date path falls back to the original
parse()method for non-standard formats, ensuring full compatibility.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
⏪ Replay Tests and Runtime
To edit these changes
git checkout codeflash/optimize-deserialize_primitive-mgcuvnkzand push.